using Admin.NET.Application.Service.Form.Dto;
using Admin.NET.Core.Service;
using Furion.DatabaseAccessor;
using Mapster;
using System.IO;

namespace Admin.NET.Application;

/// <summary>
/// 表单服务
/// </summary>
[ApiDescriptionSettings(Order = 460)]
public class SysCustomFormService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<SysCustomForm> _sysCustomFormRep;
    public SysCustomFormService(SqlSugarRepository<SysCustomForm> sysCustomFormRep)
    {
        _sysCustomFormRep = sysCustomFormRep;
    }
    //public async Task<dynamic> SaveForm(CustomFormInput input)
    //{
    //    var wwwroot = App.WebHostEnvironment.WebRootPath;
    //    await File.WriteAllTextAsync(Path.Combine(wwwroot, "admin\\test.html"), input.Html);
    //    return "";
    //}
    public async Task<dynamic> GetForm()
    {
        var wwwroot = App.WebHostEnvironment.WebRootPath;
        return await File.ReadAllTextAsync(Path.Combine(wwwroot, "admin\\test.html"));
    }
    /// <summary>
    /// 获取列表
    /// </summary>
    /// <returns></returns>
    [DisplayName("获取列表")]
    public async Task<List<CustomFormTreeOutput>> GetList([FromQuery] CustomFormInput input)
    {
        //
        var lst = new List<SysCustomForm>();
        var iSugarQueryable = _sysCustomFormRep.AsQueryable().OrderBy(u => u.Id);

        // 带条件筛选时返回列表数据
        if (!string.IsNullOrWhiteSpace(input.Name))
        {
            lst = await iSugarQueryable.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name))
                                       .ToListAsync();
        }
        else
        {
            lst = await iSugarQueryable.ToTreeAsync(u => u.Children, u => u.Pid, input.Id);
        }
        //
        return lst.Adapt<List<CustomFormTreeOutput>>();
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Add"), HttpPost]
    [DisplayName("增加")]
    public async Task<long> Add(AddCustomFormInput input)
    {
        input.Html = "";
        input.JS = "";
        var newCustomForm = await _sysCustomFormRep.AsInsertable(input.Adapt<SysCustomForm>()).ExecuteReturnEntityAsync();
        return newCustomForm.Id;
    }
    /// <summary>
    /// 获取实体
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Get")]
    [DisplayName("获取实体")]
    public async Task<SysCustomForm> Get(long id)
    {
        return await _sysCustomFormRep.GetByIdAsync(id);
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Update"), HttpPost]
    [DisplayName("更新")]
    public async Task Update(UpdateCustomFormInput input)
    {
        var sysCustomForm = await _sysCustomFormRep.GetFirstAsync(u => u.Id == input.Id);
        //
        if (!string.IsNullOrEmpty(input.Name)) sysCustomForm.Name = input.Name;
        if (!string.IsNullOrEmpty(input.Html)) sysCustomForm.Html = input.Html;
        if (!string.IsNullOrEmpty(input.JS)) sysCustomForm.JS = input.JS;

        await _sysCustomFormRep.AsUpdateable(input.Adapt<SysCustomForm>()).IgnoreColumns(true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Delete"), HttpPost]
    [DisplayName("删除")]
    public async Task Delete(DeleteCustomFormInput input)
    {
        // 
        await _sysCustomFormRep.DeleteByIdAsync(input.Id);
    }

}